﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MyMovieInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            //SRP Violation - determine the path from the arguments
            string thePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            if (args.Length > 0 && args[0] != null)
                thePath = Path.GetDirectoryName(args[0]);

            //SRP Violation - get the list of movie files from the file system
            var moviefiles = from file in new DirectoryInfo(thePath).GetFiles()
                             where file.Extension == ".avi"
                             select file;

            if (moviefiles.Count() > 0)
            {

                string regex = @"^(?<name>.+?)\((?<year>\d{4})\)\.(?<ext>\w{3})$";
                List<Movie> myMovieList = new List<Movie>();
                foreach (var moviefile in moviefiles)
                {
                    //SRP Violation - separate the title and year from the file name
                    MatchCollection matches = Regex.Matches(moviefile.Name, 
                        regex, RegexOptions.IgnoreCase);

                    if (matches != null && matches.Count > 0)
                    {
                        string moviename = matches[0].Groups["name"].Value.Trim();
                        int movieyear = Int32.Parse(matches[0].Groups["year"].Value);

                        //SRP Violation - hit the odata feed for netflix.
                        var catalog = new NetflixService.NetflixCatalog(
                            new Uri("http://odata.netflix.com/Catalog/",
                                UriKind.Absolute));

                        var response = (from p in catalog.Titles
                                        where p.Name == moviename
                                            && p.ReleaseYear == movieyear
                                            && p.Synopsis != null
                                        select p).Take(1);
                        if (response.Count() == 1)
                        {
                            var title = response.Single<NetflixService.Title>();
                            var mymovie = new Movie()
                            {
                                FilePath = moviefile.FullName,
                                Name = title.Name,
                                CoverURL = title.BoxArt.LargeUrl,
                                Synopsis = title.ShortSynopsis,
                                Year = title.ReleaseYear
                            };


                            //TODO: Persisit the data somewhere, flat file
                            //SRP Violation - Persist the data
                            myMovieList.Add(mymovie);
                        }
                        else
                        {
                            //SRP Violation - log the issue
                            Console.WriteLine(String.Format("No movie match found: {0}", moviefile.FullName));
                        }
                    }
                    else
                    {
                        //log the issue
                        Console.WriteLine(String.Format("Improper filename syntax: {0}", moviefile.FullName));
                    }
                }

                Console.WriteLine("Found Movies");
                myMovieList.Where(p => p.Synopsis != null).ToList().ForEach(e => PrintMovieToConsole(e));

            }
            else
            {
                Console.WriteLine(String.Format("No files found in \"{0}\".", thePath));
            }
            
            //pause
            Console.ReadKey();
        }

        #region Pretty Console Printer
        private static void PrintMovieToConsole(Movie movie)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("{0} - {1}", movie.Name, movie.Year);
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("{0}\n{1}\n{2}\n\n", movie.Synopsis, movie.CoverURL, movie.FilePath);
        }
        #endregion
    }


    class Movie
    {
        public string Name { get; set; }
        public int? Year { get; set; }
        public string Synopsis { get; set; }
        public string CoverURL { get; set; }
        public string FilePath { get; set; }
    }
}
